/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-06-22
 * Time: 23:27
 */
import java.util.*;
//https://www.nowcoder.com/questionTerminal/1a834e5e3e1a4b7ba251417554e07c00?answerType=1&f=discussion
public class Solution2 {
    public double Power(double base, int exponent) {
        //处理负数次幂，将base变成它的倒数
        //exponent改为正数
        if (exponent < 0) {
            base = 1 / base;
            exponent = -exponent;
        }
        return pow(base, exponent);
    }

    //快速幂
    public static double pow(double x, int y) {
        double res = 1;
        while (y != 0) {
            if ((y & 1) != 0) {
                res *= x;;
            }
            x *= x;;
            y >>= 1;
        }
        return res;
    }
}
